/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Old developer team
 * @version 1.1
 */

package wads.drawing.node;

import wads.drawing.line.CaseLink;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import wads.app.BaseWADDialog;
import wads.app.CaseEditor;
import wads.app.UserNodeDialog;

public class UserNode extends CaseNode {
//  private static final long serialVersionUID = 8048617742478984054L;
  
//  public Properties nodeProp;
  private transient BaseWADDialog wNodeDialog;

//  public static final String NODE_NAME_KEY_NAME = "name";

  private ImageIcon icon = new ImageIcon("images/user.png");
//  public static final int DEFAULT_NODE_ICON_WIDTH = 30;
//  public static final int DEFAULT_NODE_ICON_HEIGHT = 30;
  private int gap = 10;

  private String username = "";
  
  public boolean group_member_flag = false;
  protected boolean approved_status = true;
  protected GroupNode parentGroup = null;
  protected int distx = 0;
  protected int disty = 0;

  public UserNode(Point anchor) {
    super(anchor, "");

    id = ++idCount;
//    nodeProp = new Properties();

//    super.initContent();

    setWidth(DEFAULT_NODE_ICON_WIDTH);
    setHeight(DEFAULT_NODE_ICON_HEIGHT);
  }

  @Override
  public void draw(Graphics2D g2d) {
    //Init normal box dimension
    int normBoxWidth = CaseNode.DEFAULT_NODE_ICON_WIDTH;
    int normBoxHeight = CaseNode.DEFAULT_NODE_ICON_HEIGHT;
	
    //Init the big box dimension
    //int bigBoxWidth=0;
    //int bigBoxHeight=0;

    //Init small box height
    int smallBoxHeight = 15;
    //int smallBoxWidth = 40;

    //Init big box min height
    //int minBigBoxHeight = 30;

    //Init ascender line
    //int ascLineStartX = this.x + 2*this.gap;
    int ascLineStartY = getStartY() + smallBoxHeight + this.gap;

    //Get the text bounds
    g2d.setColor(Color.black);
    //Get bounds of the text
    drawTextAlign(g2d, getName(), ascLineStartY+10, this.gap);

    setNodeShapeBounds2D(getStartX(), getStartY(), DEFAULT_NODE_ICON_WIDTH, DEFAULT_NODE_ICON_HEIGHT);
    removeAllComponents();

    Rectangle2D box = new Rectangle2D.Float(getStartX(), getStartY(), normBoxWidth, normBoxHeight);
    append(box);
    g2d.drawImage(icon.getImage(), getStartX(), getStartY(), DEFAULT_NODE_ICON_WIDTH, DEFAULT_NODE_ICON_HEIGHT, icon.getImageObserver());

    super.draw(g2d);
  }// end of draw()
  
  @Override
  public String toString() {
    String connLinks = "(";
    for(CaseLink connLink: getConnectedLinks()) {
      connLinks += ("*"+connLink.getId()+" ");
    }
    connLinks += ")";
    
    return "[Node ID: "+getId()
            +", User: "+getName()
            +", Connected Links: "+connLinks
            +"]";
  }
  
//  @Override
//  public CaseNodeDialog createDialog(){ return null; }

  //add for specific dialog ability
  //the code of following two methods is the same in any WADCaseNode
  //except code at --- and end with ---
  
  @Override
  public void showDialog(CaseEditor pf){
    wNodeDialog = new UserNodeDialog(pf, this);

    //Set the dialog of this node
    //nodeDialog is declared in CaseNode
    if ((wNodeDialog != null) && (wNodeDialog instanceof BaseWADDialog)) {
      //init the displaying properties
//      wNodeDialog.setProperties(nodeProp);
      wNodeDialog.initDisplay();
      wNodeDialog.setVisible(true);
    }
  }

  @Deprecated
  @Override
  public void closeDialog(int returnValue) {
    if (returnValue == JOptionPane.OK_OPTION) {
//      this.nodeProp = wNodeDialog.getProperties();
    }
  }
  
  @Deprecated
  public void setNodeToGroupMember(boolean flag) {
    group_member_flag = flag;
  }
  
  @Deprecated
  public boolean isGroupMember() {
    return group_member_flag;
  }
  
  @Deprecated
  public void setGroupParent(GroupNode pg) {
    parentGroup = pg;
  }
  
  @Deprecated
  public GroupNode getGroupParent() {
    return parentGroup;
  }
  
  @Deprecated
  public void setGroupStartXYDistance(int dx, int dy) {
    distx = dx;
    disty = dy;
  }
  
  @Deprecated
  public int getGroupStartXDistance() {
    return distx;
  }
  
  @Deprecated
  public int getGroupStartYDistance() {
    return disty;
  }
  
  public void setNodeApprovedStatus(boolean as) {
    approved_status = as;
  }
  
  /**
   * @return Return each node's Approval authority. -true: can approve any document in that activity., -false: can't approve.
   */
  public boolean getNodeApprovedStatus() {
    return approved_status;
  }
  
  public void setNodeIconImage(ImageIcon ii) {
    icon = ii;
    System.gc();
  }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
}// end of UserNode Class